package com.parkreport.report.service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.jfree.data.time.Day;

import com.common.jdbc.DB4ObjUtil;
import com.common.report.service.ReportService;
import com.common.report.vo.ReportCountDataVO;
import com.common.report.vo.ReportTitleVO;
import com.common.util.StringUtil;
import com.parkreport.vo.ReportParamVO;

/**
 * 平均每辆车人数
 * @author Administrator
 *
 */
public class TicketCarAvgReportService extends ReportService {

	private long carTicket = 1L;
	List<ReportCountDataVO> subList = new ArrayList<ReportCountDataVO>();
	
	
	@Override
	public List findDatas(HttpServletRequest req)  {
		startDate = req.getParameter("startDate");
		endDate = req.getParameter("endDate");
		if(StringUtil.isBlank(startDate)&&StringUtil.isBlank(endDate)){
			Day d = new Day();
			String date = d.getYear()+"-"+StringUtil.keeperStr(String.valueOf(d.getMonth()), 2, "0")+"-"+d.getDayOfMonth();
			startDate =date;
			endDate = date;
			req.setAttribute("startDate", startDate);
			req.setAttribute("endDate", endDate);
		}
		
		StringBuilder sql = new StringBuilder("");
		ReportParamVO paramVO = new ReportParamVO(startDate,endDate);
		sql.append("select ca.CityName as ments, pa.AREA_NAME as titles, count(*) as counts ");
		sql.append(" from LPR_RECORD lpr,CAR_AREA ca,QUERY_DMB qd,PARK_DMB pd,PARK_AREA pa");
		sql.append(" where 1=1");
		sql.append(" and pd.TYPE='1'");
		sql.append(" and substr(lpr.PLATENO,1,2) =ca.carNum");
		sql.append(" and lpr.NID = qd.NID ");
		sql.append(" and qd.NID = pd.NID");
		sql.append(" and pd.PARK_AREA_ID=pa.ID");
		if(!StringUtil.isBlank(startDate)){
			sql.append(" and lpr.RQSJ>='"+startDate+"'");
		}
		if(!StringUtil.isBlank(endDate)){
			sql.append(" and lpr.RQSJ<='"+endDate+"'");
		}
		sql.append(" group by ca.CityName, pa.AREA_NAME");
		
		log.info("sql=========="+sql);
		List<ReportCountDataVO> list = DB4ObjUtil.execute4Obj(new ReportCountDataVO(), sql.toString());
		Map subMap = TicketService.getTicketSubMap(paramVO);
		for (int i = 0; i < list.size(); i++) {
			ReportCountDataVO v = list.get(i);
			//平均车辆人数=散客数/车辆数
			BigDecimal sub = (BigDecimal) subMap.get(v.getTitles()+"-"+v.getMents());
			BigDecimal vc = v.getCounts();
			BigDecimal count = new BigDecimal(1);
			if(sub!=null&&vc!=null&&sub.longValue()!=0&&vc.longValue()!=0){
				count=sub.divide(vc);
			}
			v.setCounts(count);
		}
		
		list.addAll(TicketService.getTicketGroup(paramVO));
		return list;
	}
	
	
	
	@Override
	public List<ReportTitleVO> findTitles(HttpServletRequest req)  {
		StringBuilder sql = new StringBuilder("");
		sql.append("select AREA_NAME as code,AREA_NAME as name from park_area where PID<>'0'");
		List<ReportTitleVO> list = DB4ObjUtil.execute4Obj(new ReportTitleVO(), sql.toString());
		
		return list;
	}
	
	@Override
	public List<ReportTitleVO> findMents(HttpServletRequest req)  {
		StringBuilder sql = new StringBuilder("");
		sql.append("select CityName as code,CityName as name from CAR_AREA group by CityName");
		List<ReportTitleVO> list = DB4ObjUtil.execute4Obj(new ReportTitleVO(), sql.toString());
	
		for (int i = 0; i < list.size(); i++) {
			ReportTitleVO v = list.get(i);
			String u = String.valueOf(mentMap.get("小计_"+v.getCode()));
			v.setUnit(u);
		}
		Collections.sort(list,new Comparator<ReportTitleVO>(){
			public int compare(ReportTitleVO obj1,ReportTitleVO obj2){
				if(StringUtil.isBlank(obj1.getUnit())&&StringUtil.isBlank(obj2.getUnit())){
					return 0;
				}
				//return order1-order2;
				if(StringUtil.isBlank(obj1.getUnit())){
					return 1;
				}
				
				if(StringUtil.isBlank(obj2.getUnit())){
					return -1;
				}
				int order1 = Integer.parseInt(obj1.getUnit());
				int order2 = Integer.parseInt(obj2.getUnit());
				return order2-order1;
			}
		});
		list.add(new ReportTitleVO("合   计","合计"));
		return list;
	}
}
